<?php


/** ensure this file is being included by a parent file */
defined('_JEXEC') or die('Restricted access');
?>

		<?php
		echo "<form action='{$this->formAction}' method='post' name='adminForm' id='adminForm'>";
		?>
		<table class="adminheading">
		<tr>
            <th class='helpdesk_tickets'>
            <?php echo HelpDeskText::_( 'Tickets' ); ?>
            </th>            
          <td class='right'>
          	<?php echo HelpDeskText::_( 'Search' ); ?>:
          </td>
          <td class="input">
          	<?php echo $this->search; ?>
          </td>
      		<?php
				for ($f=0; $f<count($this->filterLists); $f++) {
		          	echo "<td class='right'>";
					echo $this->filterLists[$f];
					echo "</td>";
				}
			?>
		</tr>
		</table>

		<table class="adminlist">
		<thead>
            <tr>
                <th width="20">
                <input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this->items ); ?>);" />
                </th>
                <th class="title">
                <?php echo HelpDeskHTML::sortList( HelpDeskText::_( 'ID' ), "id", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?>
                </th>                
                <th class="title">
                	<?php echo HelpDeskHTML::sortList( HelpDeskText::_( 'Subject' ), "subject", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?>
                	+
                	<?php echo HelpDeskHTML::sortList( HelpDeskText::_('Category'), "categoryid", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?>
                </th>
                <th class="title">
                <?php echo HelpDeskHTML::sortList( HelpDeskText::_( 'From' ), "from", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?>
                </th>
                <th class="title">
                <?php echo HelpDeskHTML::sortList( HelpDeskText::_( 'Follow-up' ), "followup", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?>
                </th>
                <th class="title">
                <?php echo HelpDeskHTML::sortList( HelpDeskText::_( 'Last Modified' ), "lastmodified", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?><br />
                <?php echo HelpDeskHTML::sortList( HelpDeskText::_( 'and' )." ".HelpDeskText::_( 'Created Date' ), "created", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?>
                </th>
                <th class="title">
                <?php echo HelpDeskHTML::sortList( HelpDeskText::_( 'Status' ), "status", $this->order_dir, $this->order, HelpDeskRequest::getVar( 'task' ) ); ?>
                <?php echo "<br />".HelpDeskText::_( 'and' )." ".HelpDeskText::_( 'Responses' ); ?>
                </th>
                <th class="title">
                <?php echo HelpDeskText::_( 'Feedback' ); ?>
                </th>
            </tr>
		</thead>
        <tbody>
		<?php
        $k = 0;
		$today 			= date( "Y-m-d" );
		$yesterday_time	= time() - (24 * 60 * 60); // timestamp - 24 hours * 60 mins * 60secs
		$yesterday		= date( "Y-m-d", $yesterday_time ); 
        for ($i=0, $n=count( $this->items ); $i < $n; $i++)
        {
            $r =& $this->items[$i];
            $checked    = HelpDeskHTML::gridId( $i, $r->id );
        	$published 	= HelpDeskHTML::gridPublished( $r, $i );
            $link = HelpDeskRoute::_( 'index2.php?option='.$option.'&controller='.$this->_name.'&task=view&cid[]='. $r->id );

            $last_modified 	= HelpDeskHTML::date($r->last_modified_datetime, "%a, %d %b %Y");
            $created 		= HelpDeskHTML::date($r->created_datetime, "%a, %d %b %Y");
            $responses 		= 0;

            $database = HelpDeskConfig::getDBO();
            // if there are ticket responses, grab a count of them
            $query = " SELECT COUNT(*) FROM #__helpdesk_messages AS msg "
                    ." WHERE msg.ticketid = '$r->id' "
                    ." AND msg.userid_from != '$r->sender_userid' "
                    ." ORDER BY msg.datetime ASC"
                    ;
            $database->setQuery($query);
            $responses = $database->loadResult();
    
	        echo "<tr class='row".$k."'>";
				echo "<td>$checked</td>";
	            echo "<td class='noborder'>"; echo "<a href='".$link."'>$r->id</a>"; echo "</td>";
	            echo "<td class='noborder'>";
	            
	                echo "<a href='".$link."'>"; 
	                echo "<strong>";
	                echo stripslashes($r->subject);
	                echo "</strong>";
	               
	                echo "<br/>";
	                 
	                echo HelpDeskText::_( $r->categoryname );
	                echo "</a>";
	            echo "</td>";
	            echo "<td class='noborder'>"; 
	                $user = HelpDeskConfig::getUser( (int) $r->sender_userid );
	                echo $user->name." [".$user->username."]<br />";
	                echo stripslashes($r->sender_email);
	                echo " [";
	                echo "<a href='index2.php?option=com_users&task=view&search=".urlencode($r->sender_email)."'>"; 
	                echo $r->sender_userid;
	                echo "</a>";
	                echo "]";
	                
	                // Fire a plugin here to allow special info to be displayed, such as a list of active subscriptions                
					$dispatcher = HelpDeskHelperPlugin::getInstance();
					$dispatcher->trigger( 'onDisplayFromColumn', array( $r, $user ) );
	
	                
	            echo "</td>";
	            echo "<td class='noborder'>";
	                $ftask 	= $r->followup ? 'nofollowup' : 'followup';
	                $img 	= $r->followup ? 'tick.png' : 'publish_x.png';
	                $alt 	= $r->followup ? HelpDeskText::_( 'Follow-up' ) : HelpDeskText::_( 'Do Not Follow-up' );
					$href = '
							<a href="javascript:void(0);" onclick="return listItemTask(\'cb'. $i .'\',\''. $ftask .'\')" title="'. $alt .'">
							<img src="images/'. $img .'" border="0" alt="'. $alt .'" /></a>'
							;
	                ?>
	                <center>
	                <span class="href">
	                <?php echo $href; ?>
	                </span>
	                </center>
	                <?php 
	            echo "</td>";
	            echo "<td class='noborder' nowrap><center>";
	                if ($last_modified == $today) {
	                    echo HelpDeskHTML::date($r->last_modified_datetime, "%I:%M%p"); // strftime formatting
	                } elseif ($last_modified == $yesterday) {
	                    echo HelpDeskText::_( 'Yesterday' ).", ".HelpDeskHTML::date($r->last_modified_datetime, "%I:%M%p"); // strftime formatting
	                } else {
	                    echo HelpDeskHTML::date($r->last_modified_datetime, "%a, %d %b %Y, %I:%M%p"); // strftime formatting
	                }
	                
	                echo "<br />---<br />";
	
	                if ($created == $today) {
	                    echo HelpDeskHTML::date($r->created_datetime, "%I:%M%p"); // strftime formatting
	                } elseif ($created == $yesterday) {
	                    echo HelpDeskText::_( 'Yesterday' ).", ".HelpDeskHTML::date($r->created_datetime, "%I:%M%p"); // strftime formatting
	                } else {
	                    echo HelpDeskHTML::date($r->created_datetime, "%a, %d %b %Y, %I:%M%p"); // strftime formatting
	                }
	            echo "</center></td>";
	            echo "<td class='noborder'><center>"; 
	                echo HelpDeskHelperHelpDesk::getTicketStatusImage($r->status);
	                if ($r->closed_datetime > 0) {
	                    echo "<br />".HelpDeskHTML::date($r->closed_datetime, "%a, %d %b %Y, %I:%M%p");					
	                } else {
	                    echo "<br />".HelpDeskHelperHelpDesk::getTicketStatus($r->status); 
	                }
	                
	                echo "<br />---<br />";
	                
	                echo $responses." "; echo $responses == 1 ? HelpDeskText::_( 'Response' ): HelpDeskText::_( 'Responses' );
	                
	            echo "</center></td>";
	            echo "<td class='noborder'><center>"; echo HelpDeskHelperHelpDesk::getFeedbackRatingImage($r->feedback_rating)."<br />".HelpDeskHTML::date($r->feedback_datetime, "%a, %d %b %Y, %I:%M%p"); echo "</center></td>";
	        echo "</tr>";
        
            if (isset($r->description) && strlen($r->description) > 1)  
			{ 
		        echo "<tr class='row".$k."'>";
	            echo "<td class='topnowrap' valign='top' nowrap>";
					$text_display = "[ + ]";
					$text_hide = "[ - ]";
					$onclick = "displayDiv(\"description_{$r->id}\", \"showhidedescription_{$r->id}\", \"{$text_display}\", \"{$text_hide}\");";
					echo "<span class='href' id='showhidedescription_{$r->id}' onclick='{$onclick}'>{$text_display}</span>";
	            echo "</td>";
	            echo "<td colspan='10'>"; 
					echo "<div id='description_{$r->id}' style='display: none;'>";
					echo nl2br( stripslashes( $r->description ) );
					echo "</div>";
	            echo "</td>";
		        echo "</tr>";
			}
        	$k = 1 - $k;
        }
		
		if (!$this->items) {
			echo "<tr class='row".$k."'>";
				echo "<td class='topnowrap' colspan='10'>";
				echo HelpDeskText::_( 'None' );
				echo "</td>";
			echo "</tr>";		
		}
        ?>
        </tbody>
        <tfoot>
        	<tr>
                <td colspan='10'>
                    <div class="pagination">
                    <?php echo $this->pagination->getListFooter(); ?>
                    </div>            
                </td>
            </tr>
        </tfoot>

		</table>
                
		<input type="hidden" name="option" value="<?php echo $this->formOption; ?>" />
		<input type="hidden" name="controller" value="<?php echo $this->formController; ?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="id" value="" />
		<input type="hidden" name="boxchecked" value="" />
		<input type="hidden" name="order" value="<?php echo $this->order; ?>" />
		<input type="hidden" name="order_dir" value="<?php echo $this->order_dir; ?>" />
        
		<?php
		echo $this->formValidate;
		echo "</form>";
		?>